package cn.microservices.cloud.gateway.interpector.ip;

import cn.microservices.cloud.gateway.util.WebFluxUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

@Component

public class IpInterceptor {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private LocalIpInterceptor localIpInterceptor;


    /**
     * ip是否非法
     *
     * @param request 访问的请求
     * @return true 拦截成功 false拦截失败
     */
    public boolean isIllegalByIp(ServerHttpRequest request) {

        String ip = WebFluxUtil.getRealIp(request);

        try {


            /**
             * 限制本地的某个IP频率
             */
            if (!localIpInterceptor.isPass(request)) {
                log.warn("IP={} 已经被禁止访问", ip);
                return true;
            }


        } catch (Exception e) {
            log.error("Ip={} 限制访问失败", ip, e);

        }

        return false;

    }
}
